package level;

import game.util.FileUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Jevgeni Krutov
 *
 */
public class LevelDownloader {
	private static final String ALL_LEVELS_LIST = "http://dijkstra.cs.ttu.ee/~t103923/levelRedactor/levels.txt";
	private static final String LEVELS_FOLDER = "http://dijkstra.cs.ttu.ee/~t103923/levelRedactor/levels/";
	
	/**
	 * 
	 * @return list of level names
	 */
	public static List<String> getLevelList() {
		List<String> levelList = new ArrayList<String>();
		try {
			URL url = new URL(ALL_LEVELS_LIST);
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
			
			String line;
			while ((line = in.readLine()) != null) {
				levelList.add(line);
			}
			in.close();
			
		} catch (MalformedURLException e) {
			System.out.println("Malformed URL: " + e.getMessage());
		} catch (IOException e) {
			System.out.println("I/O Error: " + e.getMessage());
		}
		return levelList;
	}
	
	/**
	 * 
	 * @param levelName
	 * @return level specified as text file
	 */
	public static String[] getLevel(String levelName) {
		return FileUtil.readFileFromNetworkFolder(LEVELS_FOLDER, levelName);
	}
}
